package parser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

public class RouterParser extends Parser {
   
   @Override
   void parser() throws IOException {
      setDir("Routers");
      
      if (dir.isDirectory()) {
         for (File f : dir.listFiles()) {
            parse(dir, f);
         }
      }
      writeMapToFile();
   }
   
   void parser(String directory) throws IOException {
      setDir(directory);
      
      if (dir.isDirectory()) {
         for (File f : dir.listFiles()) {
            parse(dir, f);
         }
      }
   }
   
   private void writeMapToFile() {
      
      for (String key : topicToTrainDocument.keySet()) {
         try {
            Writer output = null;
            File file = new File("topics/" + key.toUpperCase() + "_TRAINING_" + topicToTrainDocument.get(key).size() + ".txt");
            output = new BufferedWriter(new FileWriter(file));
            
            for (String s : topicToTrainDocument.get(key)) {
               output.write(s + "\n");
            }
            
            output.close();
            System.out.println("File has been written");
            
         }
         catch (Exception e) {
            System.out.println("Could not create file");
         }
      }
      
      for (String key : topicToTestDocument.keySet()) {
         try {
            Writer output = null;
            File file = new File("topics/" + key.toUpperCase() + "_TEST_" + topicToTrainDocument.get(key).size() + ".txt");
            output = new BufferedWriter(new FileWriter(file));
            
            for (String s : topicToTestDocument.get(key)) {
               output.write(s + "\n");
            }
            
            output.close();
            System.out.println("File has been written");
            
         }
         catch (Exception e) {
            System.out.println("Could not create file");
         }
      }
      
   }
   
   private void parse(File dir, File f) throws IOException {
      BufferedReader buffer = new BufferedReader(new FileReader(dir.getName() + "/" + f.getName()));
      String line = buffer.readLine();
      
      try {
         while (line != null) {
            if (line.contains("<REUTERS TOPICS=\"YES\"")) {
               createDoc(buffer, line, f);
            }
            line = buffer.readLine();
         }
      }
      finally {
         buffer.close();
      }
   }
   
   private void createDoc(BufferedReader buffer, String line, File f) throws IOException {
      boolean flag = true;
      if (line.contains("LEWISSPLIT=\"TEST\"")) {
         flag = false;
      }
      StringBuilder string = new StringBuilder();
      
      String[] topic = new String[10];
      
      while (!line.contains("<TOPICS>")) {
         line = buffer.readLine();
      }
      
      line = line.replace("<TOPICS>", "");
      line = line.replace("</TOPICS>", "");
      line = line.replace("</D>", "");
      
      topic = line.split("<D>");
      
      while (!line.contains("<TITLE>")) {
         line = buffer.readLine();
      }
      string.append("\n" + line);
      
      for (String t : topic) {
         if (flag && !t.equals("") && topicToTrainDocument.get(t.toLowerCase()) == null) {
            topicToTrainDocument.put(t.toLowerCase(), new ArrayList<String>());
         }
         else if (!flag && !t.equals("") && topicToTestDocument.get(t) == null) {
            topicToTestDocument.put(t.toLowerCase(), new ArrayList<String>());
         }
      }
      
      while (!line.contains("<BODY>")) {
         line = buffer.readLine();
      }
      while (!line.contains("</BODY>")) {
         string.append("\n" + line);
         line = buffer.readLine();
      }
      string.append("\n" + line + "\n");
      
      for (String t : topic) {
         if (!t.equals("")) {
            
            String topicString = "<TOPICS>" + t + "</TOPICS>" + string;
            if (flag) {
               ArrayList<String> list = topicToTrainDocument.get(t.toLowerCase());
               list.add(topicString);
            }
            else {
               ArrayList<String> list = topicToTestDocument.get(t.toLowerCase());
               list.add(topicString);
            }
         }
      }
      
      System.out.println(string);
      return;
   }
}
